


/**
 * primitive Cylinder 大量加载
 * 
 */
import Primitive from "../Primitive"

const DEFAULT_STYLE = {

}

export default class CylinderPrimitive extends Primitive {
  /**
   * 
   * @param {*} position 
   * @param {*} options 
   */
  constructor(positions, options = {}) {
    super()
    this._positions = positions
    this._top = options.top || 0
    this._bottom = options.bottom || 0
    this._height = options.height || 0
    this._type = 'cylinder_primitive'
    this._style = {
      ...DEFAULT_STYLE,
      ...options
    }

    this.delegate = new Cesium.Primitive({
      geometryInstances: new Cesium.GeometryInstance({
        geometry: this.createGeometry()
      }),
      modelMatrix: Cesium.Transforms.headingPitchRollToFixedFrame(
        this._positions,
        new Cesium.HeadingPitchRoll(
          Cesium.Math.toRadians(this._heading),
          Cesium.Math.toRadians(this._pitch),
          Cesium.Math.toRadians(this._roll)
        )
      ),
      releaseGeometryInstances: false
    })

    this.mounted()
  }

  /**
   * 初始化
   */

  mounted(){
    this.positions = this._positions
    this.top = this._top
    this.bottom = this._bottom
    this.height = this._height

    !this.delegate.appearance && this.setAppearance({ type: 'Color' })
  }
  /**
   * 创建图形
   * @returns 
   */
  createGeometry() {
    this._style.height && delete this._style.height
    this._style.top && delete this._style.top
    this._style.bottom && delete this._style.bottom

    let cylinder = new Cesium.CylinderGeometry({
      length: this._height,
      topRadius: this._top,
      bottomRadius: this._bottom,
      ...this._style
    })
    return Cesium.CylinderGeometry.createGeometry(cylinder)
  }

  /**
   * 设置geometry样式
   * @param {*} style 
   * @returns 
   */

  setStyle(style) {
    if (!style) return
    Object.assign(this._style, { ...style })
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }

  get positions(){
    return this._positions
  }

  set positions(val){
    this._positions = val
    this.setPosition()
  }

  get top(){
    return this._top
    
  }

  set top(val){
    this._top = val
    this.delegate.geometryInstances.geometry.topRadius = this._top
  }

  get bottom(){
    return this._bottom
  }

  set bottom(val){
    this._bottom = val
    this.delegate.geometryInstances.geometry.bottomRadius = this._bottom
  }

  get height(){
    return this._height
  }

  set height(val){
    this._height = val
    this.delegate.geometryInstances.geometry.length = this._height
  }
  

}
